<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>scanf</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">Standard C I/O</a> &gt; <a href="scanf.html">scanf</a>
  </div>

  <div class="name-format">
    scanf
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;stdio.h&gt;
  int scanf( const char *format, ... );
</pre>

  <p>The scanf() function reads input from <strong>stdin</strong>,
  according to the given <em>format</em>, and stores the data in the
  other arguments. It works a lot like <a href=
  "printf.html">printf</a>(). The <em>format</em> string consists of
  control characters, whitespace characters, and non-whitespace
  characters. The control characters are preceded by a % sign, and are
  as follows:</p>

  <table class="code-table">
    <tr>
      <th class="code-table-th">Control Character</th>

      <th class="code-table-th">Explanation</th>
    </tr>

    <tr>
      <td class="code-table-td">%c</td>

      <td class="code-table-td">a single character</td>
    </tr>

    <tr>
      <td class="code-table-td">%d</td>

      <td class="code-table-td">a decimal integer</td>
    </tr>

    <tr>
      <td class="code-table-td">%i</td>

      <td class="code-table-td">an integer</td>
    </tr>

    <tr>
      <td class="code-table-td">%e, %f, %g</td>

      <td class="code-table-td">a floating-point number</td>
    </tr>

    <tr>
      <td class="code-table-td">%o</td>

      <td class="code-table-td">an octal number</td>
    </tr>

    <tr>
      <td class="code-table-td">%s</td>

      <td class="code-table-td">a string</td>
    </tr>

    <tr>
      <td class="code-table-td">%x</td>

      <td class="code-table-td">a hexadecimal number</td>
    </tr>

    <tr>
      <td class="code-table-td">%p</td>

      <td class="code-table-td">a pointer</td>
    </tr>

    <tr>
      <td class="code-table-td">%n</td>

      <td class="code-table-td">an integer equal to the number of
      characters read so far</td>
    </tr>

    <tr>
      <td class="code-table-td">%u</td>

      <td class="code-table-td">an unsigned integer</td>
    </tr>

    <tr>
      <td class="code-table-td">%[]</td>

      <td class="code-table-td">a set of characters</td>
    </tr>

    <tr>
      <td class="code-table-td">%% a percent sign</td>
    </tr>
  </table>

  <p>scanf() reads the input, matching the characters from format. When
  a control character is read, it puts the value in the next variable.
  Whitespace (tabs, spaces, etc) are skipped. Non-whitespace characters
  are matched to the input, then discarded. If a number comes between
  the % sign and the control character, then only that many characters
  will be converted into the variable. If scanf() encounters a set of
  characters, denoted by the %[] control character, then any characters
  found within the brackets are read into the variable. The return
  value of scanf() is the number of variables that were successfully
  assigned values, or <strong>EOF</strong> if there is an error.</p>

  <div class="related-examples-format">
    Example code:
  </div>

  <div class="related-examples">
    <p>This code snippet repeatedly uses scanf() to read integers and
    floats from the user. Note that the variable arguments to scanf()
    are passed in by reference, as denoted by the ampersand (&amp;)
    preceding each variable:</p>
    <pre class="example-code">
 int i;
 float f;               

 while( 1 ) {
   printf( &quot;Enter an integer: &quot; );
   scanf( &quot;%d&quot;, &amp;i );             

   printf( &quot;Enter a float: &quot; );
   scanf( &quot;%f&quot;, &amp;f );             

   printf( &quot;You entered %d and then %f\n&quot;, i, f );
 }              
</pre>
  </div>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="fgets.html">fgets</a><br>
    <a href="fscanf.html">fscanf</a><br>
    <a href="printf.html">printf</a><br>
    <a href="sscanf.html">sscanf</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
